Skip to content

GH-124241: try reading /proc/self/exe to determine sys.executable#145486

Open
FFY00 wants to merge 5 commits intopython:mainfrom
FFY00:gh-124241
Open

GH-124241: try reading /proc/self/exe to determine sys.executable#145486
FFY00 wants to merge 5 commits intopython:mainfrom
FFY00:gh-124241

Conversation

@FFY00
Copy link
Member

@FFY00 FFY00 commented Mar 3, 2026

$ bash -c "exec -a echo $(pwd)/python -c 'import sys; print(sys.executable)'"
/home/anubis/git/cpython/python

Signed-off-by: Filipe Laíns <lains@riseup.net>
if not executable and os_name == 'posix':
# On Linux, try resolving the executable path via procfs
try:
executable = realpath('/proc/self/exe')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realpath() returns /proc/self/exe unmodified if it cannot resolve the symlink. I would prefer using readlink() here. But it requires more work, since Modules/getpath.c doesn't implement it here. I can help implementing readlink() in getpath.c if you want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, using readlink would also be my preference, I just went for this implementation for simplicity. We could move this to progname_to_dict (computes real_executable), but we should still try to honor program_name if it points to the same path.

FFY00 added 3 commits March 4, 2026 18:24
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
@FFY00 FFY00 requested a review from vstinner March 4, 2026 18:38
Signed-off-by: Filipe Laíns <lains@riseup.net>
@FFY00
Copy link
Member Author

FFY00 commented Mar 4, 2026

Well, this is kind of messy. It seems that several tests rely on being able to feed real_executable a fake path. Since this is then used to search for the standard library directories, we cannot easily modify the behavior without causing breakage for some embedding users.

I'm gonna see if I can find a way to do this without breaking the module path calculation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants